﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using BMS.Models;
using BMS.Models.DataAccess;
using BMS.ViewModels;

namespace BMS.Controllers
{
    public class MaintenanceController : Controller
    {
        private CompanyRepository comRepository = new CompanyRepository();
        private FMRepository fmRepository = new FMRepository();
        private SubContractorRepository subContRepository = new SubContractorRepository();
        private ClientRepository clientRepository = new ClientRepository();
        private DatabaseAccess dbAccess = new DatabaseAccess();
        private CompanyModelList comModelList = new CompanyModelList();
        private FMModelList fmModelList = new FMModelList();
        private SubContractorModelList subContModelList = new SubContractorModelList();
        private ClientModelList clientModelList = new ClientModelList();
        //
        // GET: /Maintenance/

        public ActionResult Client()
        {
            ViewBag.Message = "Client Maintenance";
            clientModelList.clientList = clientRepository.GetAllClients();
            clientModelList.fmList = fmRepository.GetFMsForDropDownList(fmRepository.GetAllFMs().OrderBy(x => x.FMID).ToList());
            clientModelList.companyList = comRepository.GetCompaniesForDropDownList(comRepository.GetAllCompanies().OrderBy(x => x.CompanyID).ToList());
            clientModelList.subContList = subContRepository.GetSubContractorsForDropDownList(subContRepository.GetAllSubContractors().OrderBy(x => x.SubContractorID).ToList());

            return View(clientModelList);
        }

        [HttpPost]
        public ActionResult CreateClient(ClientModelList modelList, String FMNameSV, String CompanyNameSV, String SubContractorNameSV)
        {
            ClientModel newClient = new ClientModel();
            newClient.ClientNo = (clientRepository.GetAllClients().Count + 1).ToString("00000000");
            newClient.ClientName = modelList.client.ClientName;
            newClient.Address1 = (modelList.client.Address1 == null) ? "" : modelList.client.Address1;
            newClient.Address2 = (modelList.client.Address2 == null) ? "" : modelList.client.Address2;
            newClient.Address3 = (modelList.client.Address3 == null) ? "" : modelList.client.Address3;
            newClient.Address4 = (modelList.client.Address4 == null) ? "" : modelList.client.Address4;
            newClient.Address5 = (modelList.client.Address5 == null) ? "" : modelList.client.Address5;
            newClient.TelephoneNo = (modelList.client.TelephoneNo == null) ? "" : modelList.client.TelephoneNo;
            newClient.FaxNo = (modelList.client.FaxNo == null) ? "" : modelList.client.FaxNo;
            newClient.AttentionTo = (modelList.client.AttentionTo == null) ? "" : modelList.client.AttentionTo;
            newClient.FMName = FMNameSV;
            newClient.CompanyName = CompanyNameSV;
            newClient.SubContName = SubContractorNameSV;
            newClient.ClientCreationDate = DateTime.Today;

            bool isSuccess = clientRepository.Insert(newClient);

            ViewBag.Message = dbAccess.GetConfigurationItem("ClientRegWord").ConfigurationValue + dbAccess.GetConfigurationItem("Space").ConfigurationValue
                + dbAccess.GetConfigurationItem("Hyphen").ConfigurationValue + dbAccess.GetConfigurationItem("Space").ConfigurationValue
                + dbAccess.GetConfigurationItem("Successful").ConfigurationValue;

            if (isSuccess == false)
            {
                ViewBag.Message = dbAccess.GetConfigurationItem("ClientRegWord").ConfigurationValue + dbAccess.GetConfigurationItem("Space").ConfigurationValue
                + dbAccess.GetConfigurationItem("Hyphen").ConfigurationValue + dbAccess.GetConfigurationItem("Space").ConfigurationValue
                + dbAccess.GetConfigurationItem("Fail").ConfigurationValue;
            }

            return View(newClient);
        }

        public ActionResult FacilityManagement()
        {
            ViewBag.Message = "Facility Management Maintenance";
            fmModelList.fmList = fmRepository.GetAllFMs();
            fmModelList.companyList = comRepository.GetCompaniesForDropDownList(comRepository.GetAllCompanies().OrderBy(x => x.CompanyID).ToList());
            return View(fmModelList);
        }

        [HttpPost]
        public ActionResult CreateFM(FMModelList modelList, String CompanyNameSV)
        {
            FMModel newFM = new FMModel();

            String newFMCode = modelList.fm.FMCode;
            int newFMID = fmRepository.GetAllFMs().Count + 1;

            newFM.FMID = newFMID;
            newFM.FMCode = newFMCode;
            newFM.FMName = newFMCode + newFMID.ToString("000");
            newFM.AttentionTo = (modelList.fm.AttentionTo == null) ? "" : modelList.fm.AttentionTo;
            newFM.CompanyName = CompanyNameSV;
            newFM.FMCreationDate = DateTime.Today;

            bool isSuccess = fmRepository.Insert(newFM);

            return RedirectToAction("FacilityManagement");
        }

        public ActionResult Company()
        {

            ViewBag.Message = "Company Maintenance";
            comModelList.cmList = comRepository.GetAllCompanies();

            return View(comModelList);
        }

        public ActionResult ModifyCompany(String companyID)
        {
            ViewBag.Message = "Edit Company";
            comModelList.cm = comRepository.GetCompany(int.Parse(companyID));
            return View(comModelList);
        }

        public ActionResult CompanyModification()
        {
            ViewBag.Message = "Company Modfication";
            comModelList.cmList = comRepository.GetAllCompanies();

            return View(comModelList);
        }

        

        [HttpPost]
        public ActionResult CreateCompany(CompanyModelList modelList)
        {
            CompanyModel newCompany = new CompanyModel();

            String newComName = modelList.cm.CompanyName;
            int newComID = comRepository.GetAllCompanies().Count + 1;

            newCompany.CompanyID = newComID;
            newCompany.CompanyName = newComName;
            newCompany.Address1 = (modelList.cm.Address1 == null) ? "" : modelList.cm.Address1;
            newCompany.Address2 = (modelList.cm.Address2 == null) ? "" : modelList.cm.Address2;
            newCompany.Address3 = (modelList.cm.Address3 == null) ? "" : modelList.cm.Address3;
            newCompany.Address4 = (modelList.cm.Address4 == null) ? "" : modelList.cm.Address4;
            newCompany.Address5 = (modelList.cm.Address5 == null) ? "" : modelList.cm.Address5;
            newCompany.TelephoneNo = (modelList.cm.TelephoneNo == null) ? "" : modelList.cm.TelephoneNo;
            newCompany.FaxNo = (modelList.cm.FaxNo == null) ? "" : modelList.cm.FaxNo;

            bool isSuccess = comRepository.Insert(newCompany);

            return RedirectToAction("Company");
        }

        public ActionResult SubContractor()
        {
            ViewBag.Message = "Sub Contractor Maintenance";
            subContModelList.subContractorList = subContRepository.GetAllSubContractors();

            return View(subContModelList);
        }

        [HttpPost]
        public ActionResult CreateSubContractor(SubContractorModelList modelList)
        {
            SubContractorModel newSubContractor = new SubContractorModel();

            String newSubContName = modelList.subContractor.SubContractorName;
            int newSubContID = subContRepository.GetAllSubContractors().Count + 1;

            newSubContractor.SubContractorID = newSubContID;
            newSubContractor.SubContractorName = newSubContName;
            newSubContractor.SubContractorCreationDate = DateTime.Today;

            bool isSuccess = subContRepository.Insert(newSubContractor);

            return RedirectToAction("SubContractor");
        }
    }
}
